package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * http://blog.csdn.net/lmj623565791/
 *
 * @author zhy
 */
public class MyInnerScrollView extends ScrollView {
    /**
     */
    public PullableScrollView parentScrollView;

    public MyInnerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private ScrollBottomListener scrollBottomListener;

    public interface ScrollBottomListener {
        void scrollBottom();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        try {
            if (t + getHeight() >= computeVerticalScrollRange() - 300) {
                //ScrollView滑动到底部了
                if (scrollBottomListener != null) {
                    scrollBottomListener.scrollBottom();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setScrollBottomListener(ScrollBottomListener scrollBottomListener) {
        this.scrollBottomListener = scrollBottomListener;
    }


    private int lastScrollDelta = 0;

    public void resume() {
        overScrollBy(0, -lastScrollDelta, 0, getScrollY(), 0, getScrollRange(), 0, 0, true);
        lastScrollDelta = 0;

    }

    int mTop = 10;

    /**
     * 将targetView滚到最顶端
     */
    public void scrollTo(View targetView) {
        int oldScrollY = getScrollY();
        int top = targetView.getTop() - mTop;
        int delatY = top - oldScrollY;
        lastScrollDelta = delatY;
        overScrollBy(0, delatY, 0, getScrollY(), 0, getScrollRange(), 0, 0, true);
    }

    private int getScrollRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = Math.max(0, child.getHeight() - (getHeight()));
        }
        return scrollRange;
    }

    int currentY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.v("MyInnerScrollView", "method onInterceptTouchEvent() called.parentScrollView=" + parentScrollView);
        if (parentScrollView == null) {
            return super.onInterceptTouchEvent(ev);
        } else if (parentScrollView.isInnerScroll()) {
            Log.v("MyInnerScrollView", "parentScrollView.isInnerScroll is true.");
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                // 将父scrollview的滚动事件拦截
                currentY = (int) ev.getY();
                setParentScrollAble(false);
                return super.onInterceptTouchEvent(ev);
            } else if (ev.getAction() == MotionEvent.ACTION_UP) {
                // 把滚动事件恢复给父Scrollview
                setParentScrollAble(true);
            } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            }
        } else {
            setParentScrollAble(true);
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean t = false;
        View child = getChildAt(0);
        if (parentScrollView != null) {
            if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                int height = child.getMeasuredHeight();
                height = height - getMeasuredHeight();


// System.out.println("height=" + height);
                int scrollY = getScrollY();
// System.out.println("scrollY" + scrollY);
                int y = (int) ev.getY();

// 手指向下滑动
                //y增大，手指向下滑动
                if (currentY < y) {
                    if (scrollY <= 0) {
// 如果向下滑动到头，就把滚动交给父Scrollview
                        setParentScrollAble(true);
                        return false;
                    } else {
                        setParentScrollAble(false);
                    }
                }
                //y减小，手指向上滑动
                else if (currentY > y) {


                    if (scrollY >= height) {

// 如果向上滑动到头，就把滚动交给父Scrollview
                        setParentScrollAble(true);
                        return false;
                    } else {

                        setParentScrollAble(false);
                    }
                }
                currentY = y;
            }


        }
        return super.onTouchEvent(ev);
    }

    /**
     * 是否把滚动事件交给父scrollview
     *
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        if (parentScrollView.isInnerScroll()) {
            parentScrollView.requestDisallowInterceptTouchEvent(!flag);
        }
    }

    public PullableScrollView getParentScrollView() {
        return parentScrollView;
    }

    public void setParentScrollView(PullableScrollView parentScrollView) {
        this.parentScrollView = parentScrollView;
    }
}
